package com.iflytek.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.iflytek.common.Result;
import com.iflytek.dto.PageDTO;
import com.iflytek.dto.UserDTO;
import com.iflytek.model.User;
import com.iflytek.service.UserService;
import com.iflytek.vo.PageVO;
import com.iflytek.vo.UserVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author h_t
 * @since 2023-09-23
 */
@RestController
@RequestMapping("/api/user")
public class UserController{
    @Autowired
    public UserService userService;

    /**
     * 管理员用户保存
     * @param userDTO
     * @return
     */
    @ApiOperation("管理员用户保存")
    @PostMapping("")
    public Result<Boolean> saveUser(@RequestBody UserDTO userDTO){

        return userService.saveUser(userDTO);
    }

    /**
     * 用户更新
     * @param userDTO
     * @return
     */
    @ApiOperation("用户更新")
    @PutMapping("")
    public Result<Boolean> updateUser(@RequestBody UserDTO userDTO){
        return userService.updateUser(userDTO);
    }

    /**
     * 用户注册
     * @param userDTO
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody UserDTO userDTO){
        return userService.register(userDTO);
    }

    /**
     * 用户登录
     * @param userDTO
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<UserVO> login(@RequestBody UserDTO userDTO, HttpServletRequest request){
        return userService.login(userDTO,request);
    }

    /**
     * 查找所有管理用户（非分页）
     * @return
     */
    @ApiOperation("查找所有管理用户（非分页）")
    @GetMapping("")
    public Result<List<UserVO>> findAll(){
        return userService.findAll();
    }

    /**
     * 根据id删除用户信息
     * @param id
     * @return
     */
    @ApiOperation("根据id删除用户信息")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteUser(@PathVariable("id") Long id){

        return userService.deleteUser(id);

    }

    /**
     * 根据id获取用户信息
     * @param id
     * @return
     */
    @ApiOperation("根据id获取用户信息")
    @GetMapping("/{id}")
    public Result<UserVO> findById(@PathVariable("id") Long id){
        return userService.findById(id);
    }

    /**
     * 用户密码重置
     * @param userDTO
     * @return
     */
    @ApiOperation("用户密码重置")
    @PostMapping("/reset")
    public Result<Boolean> reset(@RequestBody UserDTO userDTO){
        return userService.reset(userDTO);
    }


    /**
     * 用户信息导出Excel表格
     * @param response
     * @throws IOException
     */
    @ApiOperation("用户信息导出Excel表格")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException{
        List<Map<String, Object>> list = CollUtil.newArrayList();

        List<User> all = userService.list();
        for (User user:all){
            Map<String, Object> row1 = new LinkedHashMap<>();
            row1.put("名称",user.getUsername());
            row1.put("手机",user.getPhone());
            row1.put("邮箱",user.getEmail());
            list.add(row1);
        }
        //2.写Excel
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(list,true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("用户信息","UTF-8");
        response.setHeader("Content-Disposition","attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out,true);
        writer.close();
        IoUtil.close(System.out);
    }

    /**
     * 更新账户余额
     * @param money
     * @return
     */
    @ApiOperation("更新账户余额")
    @PutMapping("/account/{money}")
    public Result<Boolean> recharge(@PathVariable("money") BigDecimal money, HttpServletRequest request){
        return userService.recharge(money,request);
    }

    /**
     * 分页查询
     * @param pageDTO
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageVO<UserVO>> userListPage(PageDTO<UserDTO> pageDTO){
        return userService.userListPage(pageDTO);
    }
}